﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Linq;
using Yxinmt.Interface;
using Yxinmt.Utility;

namespace Yxinmt.Windows.Meeting
{
    public class SelectFile : BaseWindow, IWindow
    {
        Guid _MeetingId { get; set; }

        Entity.Meeting _Meeting { get; set; }

        public Entity.FileInfo SelectSubjectFile { get; set; }
        Components.ScrollViewerComponent _Panel { get; set; }

        public List<string> _VideoFormatList { get; set; }

        public SelectFile(Window window, Dictionary<string, object> parameters)
        {
            Window = window;
            _Parameters = parameters;
            IsDialogWindow = true;

            _MeetingId = parameters.GetValue<Guid>("MeetingId");
            _VideoFormatList = parameters.GetValue<List<string>>("VideoFormatList");

            InitSet(GetType().Name);

            initView();

            this.SetWindow();

            initLoad();
        }

        Dictionary<string, object> _FileDirPropery { get; set; }
        Dictionary<string, object> _FilePropery { get; set; }

        void initView()
        {
            var properties = _View.GetValue<List<Dictionary<string, object>>>("properties");
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
            foreach (var p in properties)
            {
                var name = p.GetStringValue("name");
                if (name == "FileDirPropery") _FileDirPropery = p;
                else if (name == "FilePropery") _FilePropery = p;
                else list.Add(p);
            }
            _View["properties"] = list;
        }

        void initLoad()
        {
            _Panel = this.GetComponent("FileSelectPanel") as Components.ScrollViewerComponent;
        }

        public override void DataLoad()
        {
            base.DataLoad();

            _Meeting = RequestService.GetMeetingSubjects(_MeetingId);

            if (_Meeting != null && _Meeting.Subjects != null)
            {
                _DataList = (from a in _Meeting.Subjects
                             select new Entity.SelectDirFile()
                             {
                                 FileId = a.SubjectId,
                                 FileName = a.Name,
                                 FileList = getFileList(a.Files)
                             }).ToList();
            }

            var noteDir = new Entity.SelectDirFile();
            noteDir.FileId = Guid.NewGuid();
            noteDir.FileName = "文件批注";
            noteDir.DirList = new List<Entity.SelectDirFile>();

            var whiteboardDir = new Entity.SelectDirFile();
            whiteboardDir.FileId = Guid.NewGuid();
            whiteboardDir.FileName = "电子白板";
            whiteboardDir.FileList = new List<Entity.SelectDirFile>();

            var recordDir = new Entity.SelectDirFile();
            recordDir.FileId = Guid.NewGuid();
            recordDir.FileName = "会议笔记";
            recordDir.FileList = new List<Entity.SelectDirFile>();

            var recordList = RequestService.GetMeetingRecords(_MeetingId);
            if (recordList != null && recordList.Count > 0)
            {
                var groupby = recordList.Where(w => w.RecordType == 1).GroupBy(g => g.FileId);
                foreach (var group in groupby)
                {
                    var first = group.First();
                    noteDir.DirList.Add(new Entity.SelectDirFile()
                    {
                        FileId = group.Key,
                        FileName = first.FileName2,
                        FileList = group.Select(s => getRecordFile(s)).ToList()
                    });
                }

                recordList.ForEach(d =>
                {
                    if (d.RecordType == 2) whiteboardDir.FileList.Add(getRecordFile(d));
                    else if (d.RecordType == 3) recordDir.FileList.Add(getRecordFile(d));
                });
            }

            if (noteDir.DirList.Count > 0) _DataList.Add(noteDir);
            if (whiteboardDir.FileList.Count > 0) _DataList.Add(whiteboardDir);
            if (recordDir.FileList.Count > 0) _DataList.Add(recordDir);

            SetDataList();
        }

        Entity.SelectDirFile getRecordFile(Entity.MeetingRecord record)
        {
            return new Entity.SelectDirFile
            {
                FileId = record.RecordId,
                FileName = getFileName(record),
                File = new Entity.FileInfo()
                {
                    FileId= record.RecordId,
                    FileName = record.FileName,
                    FileType = record.FileType,
                    RecordType = record.RecordType,
                    ContentUrl = record.ContentUrl,
                    FileUrl= record.FileUrl
                }
            };
        }

        string getFileName(Entity.MeetingRecord record)
        {
            if (record.RecordType == 1)
            {
                string name = record.FileName.Replace(".png", string.Empty);
                return $"设备{record.DisplayIndex}_{name}_{record.CreateDate.ToString("yyyy-MM-dd HH:mm:ss")}.png";
            }
            return record.FileName;
        }

        List<Entity.SelectDirFile> getFileList(List<Entity.FileInfo> files)
        {
            if (files == null || files.Count == 0) return null;

            return (from a in files.Where(w => !_VideoFormatList.Contains(w.FileType))
                    select new Entity.SelectDirFile()
                    {
                        FileId = a.FileId,
                        FileName = a.FileName,
                        File = a
                    }).ToList();
        }

        List<Entity.SelectDirFile> _DataList { get; set; }

        void SetDataList()
        {
            if (_DataList == null || _DataList.Count == 0) return;

            var panel = getDirFilePropertiesPanel(_DataList, "dirfile", 50);

            if (_Panel != null)
            {
                _Panel.LoadView(panel);
                _Panel.EventLoad();
                _Panel.DataLoad();
            }
        }

        Dictionary<string, object> getDirFilePropertiesPanel(List<Entity.SelectDirFile> dataList, string parentPropertyId, int x)
        {
            if (dataList == null || dataList.Count == 0) return null;

            List<Dictionary<string, object>> dictList = new List<Dictionary<string, object>>();

            Dictionary<string, object> fileDirPropery2 = null;

            List<Entity.PropertySet> propertyList = null;

            dataList.ForEach(d =>
            {
                propertyList = new List<Entity.PropertySet>();

                propertyList.Add(new Entity.PropertySet("CloseLabel_FileDir", "Text", d.FileName));
                propertyList.Add(new Entity.PropertySet("OpenLabel_FileDir", "Text", d.FileName));

                propertyList.Add(new Entity.PropertySet("WhiteTrianle_FileDir", "X", x));
                propertyList.Add(new Entity.PropertySet("BlackTrianle_FileDir", "X", x));

                propertyList.Add(new Entity.PropertySet("Close_FileDir", "Visible", !d.IsOpenFileDir));
                propertyList.Add(new Entity.PropertySet("Close_FileDir", "name", "CloseFileDir_" + d.PropertyId));

                propertyList.Add(new Entity.PropertySet("Open_FileDir", "Visible", d.IsOpenFileDir));
                propertyList.Add(new Entity.PropertySet("Open_FileDir", "name", "OpenFileDir_" + d.PropertyId));

                fileDirPropery2 = Utils.CloneDictionary(_FileDirPropery, propertyList);
                fileDirPropery2["name"] = d.PropertyId;

                var properties = fileDirPropery2.GetValue<List<Dictionary<string, object>>>("properties");
                foreach (var p in properties)
                {
                    p["Data"] = d;
                    p["ParentPropertyId"] = parentPropertyId;
                }

                dictList.Add(fileDirPropery2);

                Dictionary<string, object> panel2 = null;

                if (d.DirList != null && d.DirList.Count > 0) panel2 = getDirFilePropertiesPanel(d.DirList, d.PropertyId, x + 40);

                if (d.FileList != null && d.FileList.Count > 0)
                {
                    if (panel2 == null)
                    {
                        panel2 = new Dictionary<string, object>();
                        panel2.Add("name", "list_" + d.PropertyId);
                        panel2.Add("type", "StackPanel");
                    }
                    properties = new List<Dictionary<string, object>>();
                    d.FileList.ForEach(f =>
                    {
                        properties.Add(GetFileProperty(f, _FilePropery, x));
                    });

                    panel2.Add("properties", properties);
                }

                if (panel2 != null) dictList.Add(panel2);
            });

            if (dictList.Count == 0) return null;

            Dictionary<string, object> panel = new Dictionary<string, object>();
            panel.Add("name", "list_" + parentPropertyId);
            panel.Add("type", "StackPanel");
            panel.Add("properties", dictList);

            return panel;
        }

        private Dictionary<string, object> GetFileProperty(Entity.SelectDirFile file, Dictionary<string, object> filePropery, int x)
        {
            List<Entity.PropertySet> propertyList = new List<Entity.PropertySet>();

            propertyList.Add(new Entity.PropertySet("CloseLabel_FileDir", "Text", file.FileName));
            propertyList.Add(new Entity.PropertySet("OpenLabel_FileDir", "Text", file.FileName));

            propertyList.Add(new Entity.PropertySet("BlackTrianle_FileDir", "X", x + 40));

            Dictionary<string, object> filePropery2 = Utils.CloneDictionary(filePropery, propertyList);

            var properties = filePropery2.GetValue<List<Dictionary<string, object>>>("properties");
            foreach (var p in properties)
            {
                p["Data"] = file;
                p["name"] = "file_" + file.PropertyId;
            }

            return filePropery2;
        }

        public void CancelDialog(IControl ctl)
        {
            Window.DialogResult = false;
        }

        public void SelectFileOk(IControl ctl)
        {
            SelectSubjectFile = ctl.Property.GetValue<Entity.SelectDirFile>("Data").File;
            Window.DialogResult = true;
        }

        public void OpenDir(IControl ctl)
        {
            OpenCloseDir(ctl, true);
        }

        public void CloseDir(IControl ctl)
        {
            OpenCloseDir(ctl, false);
        }

        public void OpenCloseDir(IControl ctl, bool isOpen)
        {
            var data = ctl.Property.GetValue<Entity.SelectDirFile>("Data");
            var listPanel = GetControl("list_" + data.PropertyId);
            if (listPanel != null)
            {
                data.IsOpenFileDir = isOpen;
                listPanel.SetVisible(isOpen);
            }
            var dir = GetComponent(data.PropertyId);
            dir.ComponentList.ForEach(e =>
            {
                var c = e.ControlList[0];
                if (c == ctl) c.SetVisible(false);
                else c.SetVisible(true);
            });
        }
    }
}
